package com.example.neusoft215gdh.androidpro_21.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.neusoft215gdh.androidpro_21.Fragement.ClubFragment;
import com.example.neusoft215gdh.androidpro_21.Fragement.IndexFragment;
import com.example.neusoft215gdh.androidpro_21.Fragement.MySelfFragment;
import com.example.neusoft215gdh.androidpro_21.Fragement.PeiXunFragement;
import com.example.neusoft215gdh.androidpro_21.Fragement.ShaiTuFragment;
import com.example.neusoft215gdh.androidpro_21.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener
{
    private LinearLayout perXun;
    private LinearLayout juLeBu;
    private LinearLayout index;
    private LinearLayout shaiTu;
    private LinearLayout mySelf;

    private ImageButton mImgperXun;
    private ImageButton mImgjuLeBu;
    private ImageButton mImgindex;
    private ImageButton mImgshaiTu;
    private ImageButton mImgmySelf;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();//监听事件
        setSelect(0);//默认选择是哪个界面进行显示

    }

    private void initEvent()
    {
        perXun.setOnClickListener(this);//培训
        juLeBu.setOnClickListener(this);//俱乐部
        index.setOnClickListener(this);//主页
        shaiTu.setOnClickListener(this);//晒图
        mySelf.setOnClickListener(this);//个人

    }

    private void initView()
    {
        perXun= (LinearLayout) findViewById(R.id.id_buttom_peixun);
        juLeBu = (LinearLayout) findViewById(R.id.id_buttom_shaitu);
        index = (LinearLayout) findViewById(R.id.id_buttom_julebu);
        shaiTu= (LinearLayout) findViewById(R.id.id_buttom_index);
        mySelf= (LinearLayout) findViewById(R.id.id_buttom_wode);

        mImgperXun = (ImageButton) findViewById(R.id.id_peixun);
        mImgjuLeBu = (ImageButton) findViewById(R.id.id_julebu);
        mImgindex = (ImageButton) findViewById(R.id.id_index);
        mImgshaiTu = (ImageButton) findViewById(R.id.id_shaitu);
        mImgmySelf= (ImageButton) findViewById(R.id.id_wode);
    }


    //管理fragement和修改颜色
    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new PeiXunFragement();
                    transaction.add(R.id.id_content, mTab01);
        } else
                {
                    transaction.show(mTab01);//如果为空就显示fragment
                }
                mImgperXun.setImageResource(R.drawable.ic_train);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                mImgjuLeBu.setImageResource(R.drawable.ic_club);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new IndexFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgindex.setImageResource(R.drawable.ic_main);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new ShaiTuFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgshaiTu.setImageResource(R.drawable.ic_post);
                break;

            case 4:
                if (mTab05==null){
                    mTab05=new MySelfFragment();
                    transaction.add(R.id.id_content,mTab05);
                }else {
                    transaction.show(mTab05);
                }
                mImgmySelf.setImageResource(R.drawable.ic_person);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_buttom_peixun:
                setSelect(0);
                break;
            case R.id.id_buttom_julebu:
                setSelect(1);
                break;
            case R.id.id_buttom_index:
                setSelect(2);
                break;
            case R.id.id_buttom_shaitu:
                setSelect(3);
                break;
            case R.id.id_buttom_wode:
                setSelect(4);
            default:
                break;
        }
    }

    private void resetImgs()
    {
        mImgperXun.setImageResource(R.drawable.ic_train_btn);
        mImgjuLeBu.setImageResource(R.drawable.ic_club_btn);
        mImgindex.setImageResource(R.drawable.ic_main_btn);
        mImgshaiTu.setImageResource(R.drawable.ic_post_btn);
        mImgmySelf.setImageResource(R.drawable.ic_person_btn);
    }

}